package me.lc.linklinkchrist.layer;

import me.lc.linklinkchrist.R;
import me.lc.linklinkchrist.core.GameSystem;
import me.lc.linklinkchrist.core.StageManager;
import me.lc.linklinkchrist.scene.GameScene;
import me.lc.linklinkchrist.scene.MainScene;
import me.lc.linklinkchrist.util.ADHelper;
import me.lc.linklinkchrist.util.AudioHelper;
import me.lc.linklinkchrist.util.GameUtil;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.game.WiGame;

/**
 * 主选单图层
 * 
 * @author Administrator
 * 
 */
public class MainMenuLayer extends Layer {

	// 主选单Nodes
	Button helpButton;
	Button timeButton;
	Button normalButton;
	Button topListButton;
	Sprite mainMenuBG;

	public MainMenuLayer() {
		// 主选单
		initMainMenu();
	}

	private void initMainMenu() {
		mainMenuBG = Sprite.make(GameSystem.menuBGTex);
		mainMenuBG.setAnchorPercent(0, 0);
		float bgX = (GameSystem.Screen_Width - GameSystem.menuBGTex.getWidth()) / 2.0f;
		float bgY = GameSystem.Screen_ActuallyHeight * 0.2625f;
		mainMenuBG.setPosition(bgX, bgY);
		addChild(mainMenuBG);

		float marginTopButtom = GameSystem.Screen_ActuallyHeight * 0.03f;
		float marginBtn = ((GameSystem.menuBGTex.getHeight() - marginTopButtom * 2.0f) - (GameSystem.btnHelpTex
				.getHeight() * 4.0f)) / 3.0f;
		float buttonX = (GameSystem.Screen_Width - GameSystem.btnHelpTex
				.getWidth()) / 2.0f;

		topListButton = Button
				.make(R.drawable.button_toplist,
						R.drawable.button_toplist_press, this,
						"onTopListButtonClicked");
		topListButton.setAnchorPercent(0, 0);
		topListButton.setPosition(buttonX, bgY + marginTopButtom);
		addChild(topListButton);

		// HelpButton
		helpButton = Button.make(R.drawable.button_help,
				R.drawable.button_help_press, this, "onHelpButtonClicked");
		helpButton.setAnchorPercent(0, 0);
		helpButton.setPosition(buttonX, bgY + marginTopButtom
				+ GameSystem.btnHelpTex.getHeight() + marginBtn);
		addChild(helpButton);

		// TimeButton
		timeButton = Button.make(R.drawable.button_timegame,
				R.drawable.button_timegame_press, this,
				"onTimeGameButtonClicked");
		timeButton.setAnchorPercent(0, 0);
		timeButton.setPosition(buttonX, bgY + marginTopButtom
				+ GameSystem.btnHelpTex.getHeight() * 2 + marginBtn * 2);
		addChild(timeButton);

		// NormalButton
		normalButton = Button.make(R.drawable.button_normalgame,
				R.drawable.button_normalgame_press, this,
				"onNormalGameButtonClicked");
		normalButton.setAnchorPercent(0, 0);
		normalButton.setPosition(buttonX, bgY + marginTopButtom
				+ GameSystem.btnHelpTex.getHeight() * 3 + marginBtn * 3);
		addChild(normalButton);
	}

	public void onHelpButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.clickItem();
				MainScene.make().showHelpLayer();
			}
		});
	}

	public void onTimeGameButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.clickItem();
				GameScene.make().startNewTimeGame();
				GameUtil.switchSceneFlipX(GameScene.make());
				ADHelper.showAd();
			}
		});
	}

	public void onNormalGameButtonClicked() {
		AudioHelper.clickItem();
		System.out.println("onNormalGameButtonClicked");
		MainScene.make().showStageMenu();

	}

	public void onTopListButtonClicked() {
		WiGame.openLeaderboards();
	}

}
